package com.example.doer.reading.AppActivities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.image.AbImageLoader;
import com.example.doer.reading.AppDataModel.CircleImageView;
import com.example.doer.reading.AppDataModel.Topic;
import com.example.doer.reading.AppDataModel.TopicReply;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import java.util.Date;

import static com.ab.util.AbToastUtil.showToast;

/**
 * Created by 严旭江 on 2017/7/11.
 */

public class TopicActivity extends AbActivity {
    //添加include下面的三个Button
    TextView like_tv;
    TextView collect_tv;
    TextView comment_tv;
    TopicReply receivedTopicReply;
    Topic receivedTopic;
    AbHttpUtil abHttpUtil;
    static int collectCount;
    static boolean isCollect;//是否收藏过
    AbImageLoader abImageLoader;
    CircleImageView circleImageView;
    static boolean isFirstCome;
    @Override
    protected void onCreate(Bundle onSavedInstancetype){
        super.onCreate(onSavedInstancetype);
        setAbContentView(R.layout.activity_topic);
        abHttpUtil = AbHttpUtil.getInstance(TopicActivity.this);
        receivedTopic = (Topic) getIntent().getSerializableExtra("topicReplyObj");
        receivedTopicReply = receivedTopic.getaTopicReply();
        ((TextView)findViewById(R.id.nameOfActivityTopic)).setText("回复: "+receivedTopic.getTopicName());
        ((TextView)findViewById(R.id.contentOfActivityTopic)).setText(receivedTopicReply.getReplyContent());

        collectCount = receivedTopicReply.getCollectCount();
        final String replyUserID = receivedTopicReply.getUserID();//Reply的UserID
        String userName = receivedTopicReply.getUserName();
        ((TextView)findViewById(R.id.userNameOfActivityTopic)).setText(userName);
        /**
         * 通过用户ID获取其他用户的用户头像
         */
        circleImageView = (CircleImageView)findViewById(R.id.userResIDOfActivityTopic);
        String headURL="http://192.168.191.1/Reading/ImageDoServlet?Action=download&Category=UserLogo&ImageName="+replyUserID;
        abImageLoader = AbImageLoader.getInstance(TopicActivity.this);
        abImageLoader.display(circleImageView,headURL,300,300);

        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TopicActivity.this,MyHomeActivity.class);
                intent.putExtra("UserID",replyUserID);
                startActivity(intent);
            }
        });

        /**
         * 根据用户的情况 设置drawableTop的图像
         */
        like_tv = (TextView)findViewById(R.id.favourBtnOfActivityTopic);
        Drawable dra1 = null;
        if(receivedTopicReply.isThumbUp()) {
            dra1 = getResources().getDrawable(R.drawable.icon_thumb_up_clicked);
            like_tv.setEnabled(false);//无法继续点赞
        }
        else{
            dra1 = getResources().getDrawable(R.drawable.icon_thumb_up_initial);
        }
        dra1.setBounds(0, 0, dra1.getMinimumWidth(), dra1.getMinimumHeight());
        like_tv.setCompoundDrawables(null,dra1,null,null);
        like_tv.setText(receivedTopicReply.getFavoCount()+"点赞");


        collect_tv = (TextView) findViewById(R.id.collectBtnOfActivityTopic);
        Drawable dra2 =null;
        if(receivedTopicReply.isCollect()) {
            dra2 = getResources().getDrawable(R.drawable.icon_collect_selected);
            isCollect = true;
        }
        else {
            dra2 = getResources().getDrawable(R.drawable.icon_collect_initial
            );
        isCollect = false;}

        dra2.setBounds(0,0,dra2.getMinimumWidth(),dra2.getMinimumHeight());
        collect_tv.setCompoundDrawables(null,dra2,null,null);
        collect_tv.setText(receivedTopicReply.getCollectCount()+"收藏");

        comment_tv = (TextView) findViewById(R.id.commentNumOfBottomTopic);
        Drawable dra3 = getResources().getDrawable(R.drawable.icon_reply_comment);
        dra3.setBounds(0,0,dra3.getMinimumWidth(),dra3.getMinimumHeight());
        comment_tv.setCompoundDrawables(null,dra3,null,null);
        comment_tv.setText(receivedTopicReply.getReplyCount()+"评论");

        //返回按键
        ((Button)findViewById(R.id.TopicReply_btnReturn)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicActivity.this.finish();
            }
        });
        //点赞
        like_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like_tv.setText((receivedTopicReply.getFavoCount()+1)+"点赞");
                Drawable newDra = getResources().getDrawable(R.drawable.icon_thumb_up_clicked);
                newDra.setBounds(0, 0, newDra.getMinimumWidth(), newDra.getMinimumHeight());
                like_tv.setCompoundDrawables(null,newDra,null,null);
                like_tv.setEnabled(false);
                getHTTPResponse(v,false);
            }
        });
        //收藏☆  功能待实现
        collect_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable newDra2 = null;
                if(isCollect){//收藏过
                    newDra2 = getResources().getDrawable(R.drawable.icon_collect_initial);
                    newDra2.setBounds(0, 0, newDra2.getMinimumWidth(), newDra2.getMinimumHeight());
                    collect_tv.setCompoundDrawables(null,newDra2,null,null);
                    collectCount-=1;
                    collect_tv.setText((collectCount)+"收藏");
                    getHTTPResponse(v,false);
                    isCollect = !isCollect;
                }
                //未收藏过
                else {
                    newDra2 = getResources().getDrawable(R.drawable.icon_collect_selected);
                    newDra2.setBounds(0, 0, newDra2.getMinimumWidth(), newDra2.getMinimumHeight());
                    collect_tv.setCompoundDrawables(null,newDra2,null,null);
                    collectCount+=1;
                    collect_tv.setText((receivedTopicReply.getCollectCount()+1)+"收藏");
                    getHTTPResponse(v,true);
                    isCollect = !isCollect;
                }
            }
        });
        //评论Button的点击监听
        comment_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TopicActivity.this,ReplyAnswerActivity.class);
                intent.putExtra("topicObjWithReply_TO_ReplyAnswer_Activity",receivedTopicReply);
                TopicActivity.this.startActivity(intent);
            }
        });
    }//end method onCreate

    /**
     *
     * @param v 点击的V
     * @param theBool  如果是点赞按钮 则也是false
     */
    private void getHTTPResponse(View v,boolean theBool){
        String UserID = MyApplication.user.getUserID();
        String UserName = MyApplication.user.getUserName();
        String toUserID = receivedTopicReply.getUserID();
        String value = receivedTopicReply.getReplyID();//三级的ID
        String des = receivedTopicReply.getReplyContent();
        String superValue = receivedTopic.getTopicID();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy--MM-dd");
        String date = sdf.format(new Date());
        String url;
        switch (v.getId()){
            case R.id.favourBtnOfActivityTopic:{//与theBool无关
                url = "http://192.168.191.1/Reading/MessageServlet?Action=ThumbUpAnswer&userID="
                +UserID+"&toUserID="+toUserID+"&userName="+UserName+"&value="+value+"&date="
                +date+"&des="+des+"&superValue="+superValue;
                abHttpUtil.get(url, new AbStringHttpResponseListener() {
                    @Override
                    public void onSuccess(int i, String s) {}
                    @Override
                    public void onStart() {}
                    @Override
                    public void onFinish() {}
                    @Override
                    public void onFailure(int i, String s, Throwable throwable) {showToast(TopicActivity.this,"连接失败");}
                });
                break;
            }
            case R.id.collectBtnOfActivityTopic:{

                if(theBool){//------------------->未收藏变为收藏
                    url = "http://192.168.191.1/Reading/MessageServlet?Action=collectAnswer&userID="
                            +UserID+"&toUserID="+toUserID+"&userName="+UserName+"&value="+value+"&date="
                            +date+"&des="+des+"&superValue="+superValue;
                    abHttpUtil.get(url, new AbStringHttpResponseListener() {
                        @Override
                        public void onSuccess(int i, String s) {

                        }
                        @Override
                        public void onStart() {}
                        @Override
                        public void onFinish() {}
                        @Override
                        public void onFailure(int i, String s, Throwable throwable) {showToast(TopicActivity.this,"连接失败");}
                    });
                }
                else{//收藏变为未收藏
                    url = "http://192.168.191.1/Reading/MessageServlet?Action=cancelCollect&userID="
                            +UserID+"&toUserID="+toUserID+"&userName="+UserName+"&value="+value+"&date="
                            +date+"&des="+des+"&superValue="+superValue;
                    abHttpUtil.get(url, new AbStringHttpResponseListener() {
                        @Override
                        public void onSuccess(int i, String s) {
                        }
                        @Override
                        public void onStart() {}
                        @Override
                        public void onFinish() {}
                        @Override
                        public void onFailure(int i, String s, Throwable throwable) {showToast(TopicActivity.this,"连接失败");}
                    });
                }
            }
            break;
            default:
                break;
        }
    }
}//end class
